package com.t235.gc.gateway;

import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Goods;
import com.t235.gc.gateway.config.WxMappingJackson2HttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class RestTempUtils {

    private RestTemplate restTemplate = new RestTemplate();
    @Autowired
    private HttpServletRequest httpServletRequest;
    //不带参数的get提交
    public <T> T getForObject(String url, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        //第一个参数是访问地址,第2个参数是访问类型,第三个参数 请求实体，封装请求头，请求内容
        //第4个参数 响应类型，根据服务接口的返回类型决定 第5个url中参数变量值
        //restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET,new HttpEntity(httpHeaders), responseType);
        return responseEntity.getBody();
    }
    //带参数的get提交
    public <T> T getForObjectByAll(String url, Class<T> responseType,Map<String, Object> map) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        //第一个参数是访问地址,第2个参数是访问类型,第三个参数 请求实体，封装请求头，请求内容
        //第4个参数 响应类型，根据服务接口的返回类型决定 第5个url中参数变量值
        //restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        //HttpEntity<Object> entity = new HttpEntity<>(map,httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET,new HttpEntity(httpHeaders), responseType,map);
        return responseEntity.getBody();
    }
    //delete提交
    public <T> T delForObject(String url, Class<T> responseType,Map<String, Object> map) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE,new HttpEntity(httpHeaders), responseType,map);
        return responseEntity.getBody();
    }

    //post提交
    public <T> T postForObject(String url, MultiValueMap requestParams, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(requestParams, httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, entity, responseType);
        return responseEntity.getBody();
    }
    //post提交 文件上传
    public <T> T postForObjectByFile(String url, MultiValueMap<String,Object> requestParams, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(requestParams, httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, entity, responseType);
        return responseEntity.getBody();
       /* HttpHeaders headers = new HttpHeaders();
        headers.put(HttpHeaders.COOKIE, getSsoCookie());
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> httpEntity=new HttpEntity<>(requestParams, headers);
        return restTemplate.postForEntity(url, httpEntity, responseType).getBody();*/
        /*HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        httpHeaders.add("appCode", "platform-device");
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(requestParams, httpHeaders);
        return restTemplate.exchange(url, HttpMethod.POST, entity, responseType).getBody();*/
        /*HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        httpHeaders.add("appCode", "platform-device");
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(requestParams, httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, entity, responseType);
        return responseEntity.getBody();*/

    }
    public <T> T postForObjectAndForm(String url, MultiValueMap requestParams, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        System.out.println(HttpHeaders.COOKIE);
        HttpEntity<MultiValueMap> entity = new HttpEntity<>(requestParams, httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, entity, responseType);
        return responseEntity.getBody();
    }
    public <T> T postForObject(String url, String jsonString, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        HttpEntity<String> entity = new HttpEntity<>(jsonString, httpHeaders);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, entity, responseType);
        return responseEntity.getBody();
    }
    public <T> T postForObject(String url, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, getSsoCookie());
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url, new HttpEntity(httpHeaders), responseType);
        return responseEntity.getBody();
    }

    public List<String> getSsoCookie() {
        Cookie[] cookies = httpServletRequest.getCookies();
        List<String> cookieList = new ArrayList<>();
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+"="+cookie.getValue());
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
            }
        }
        return cookieList;
    }
}
